package org.bluewhale.hibernate.dao;

import org.hibernate.Transaction;

import java.util.*;

public class CreateData {
	
	public static String src = "abcdefghijklmnopqrsturwxyzABCDEFGHIJKLMNOPQRSTURWXYZ1234567890_";
	
	public static Random random = new Random();
	
	public static String[] type = {"want", "readed", "reading", "owned"};
	public static String[] pics = {"http://otho.douban.com/lpic/s3529461.jpg",
									"http://otho.douban.com/lpic/s3381235.jpg",
									"http://otho.douban.com/lpic/s2988793.jpg",
									"http://otho.douban.com/lpic/s2755559.jpg"};
	
	public static String generateStr(int length) {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < length; i ++)
			buffer.append(src.charAt(Math.abs(random.nextInt()) % src.length()));
		return buffer.toString();
	}

	public static void addUser(int number) {
		SkUserDAO userSave = new SkUserDAO();
		for (int i = 0; i < number; i ++) {
			SkUser user = new SkUser();
			user.setBirthday(generateStr(8));
			user.setSchool(generateStr(8));
			user.setUserId(generateStr(8));
			user.setUserName(generateStr(8));
			if (i == 10) {
				user.setBirthday("1986-02-20");
				user.setSchool("pku");
				user.setUserId("peeking");
				user.setUserName("Liu Fei");
			}
			userSave.save(user);
		}
	}
	
	public static void addBookTagComment(int number) {
		SkUserBookDAO bookDao = new SkUserBookDAO();
		SkTagDAO tagDao = new SkTagDAO();
		for (int i = 0; i < number; i ++) {
			SkUserDAO userDao = new SkUserDAO();
			SkUser user = null;
			while (user == null)
				user = userDao.findById(random.nextInt() % 25 + 1);
			
			SkUserBook book = new SkUserBook();
			String picUrl = pics[Math.abs(random.nextInt()) % 4]; 
			book.setComment(generateStr(50));
			book.setPicUrl(picUrl);
			book.setRank(Math.abs(random.nextInt()) % 5 + 1);
			book.setSkUser(user);
			book.setBookId(Math.abs(random.nextInt()) % 50);
			book.setType(type[Math.abs(random.nextInt()) % 4]);
			//book.setSkTags(new HashSet());
			bookDao.save(book);
			
			SkTag tag = new SkTag();
			tag.setSkUser(user);
			tag.setSkUserBook(book);
			tag.setTagName(generateStr(2));
			tag.setPicUrl(picUrl);
			tagDao.save(tag);
			
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		CreateData.addUser(25);
		CreateData.addBookTagComment(200);
	}

}
